<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
// import resize from "./mixins/resize";
export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "370px",
    },
    selCompanyDataAddressList: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
      chartData: [
        { name: "其他行业", value: 43 },
        { name: "机械/设备/重工", value: 42 },
        { name: "化工/环保", value: 28 },
        { name: "电子/半导体/原器件", value: 12 },
        { name: "教育/培训", value: 12 },
        { name: "贸易/进出口", value: 11 },
        { name: "汽车/摩托车", value: 10 },
        { name: "互联网/电子商务", value: 9 },
        { name: "服装/纺织/皮革", value: 8 },
        { name: "交通/运输/物流", value: 8 },
      ],
    };
  },
  // created() {
  //   this.initChart();
  // },
  watch: {
    selCompanyDataAddressList(newVal, oldVal) {
      if (newVal.length > 0) {
        this.initChart();
      }
    },
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      const that = this;
      this.chart.setOption({
        title: {
          text: "企业行业分布TOP10",
          left: "",
          textStyle: {
            color: "#333",
            fontWeight: "bold",
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          formatter: "{b}: {c} 企业数",
        },
        xAxis: {
          type: "category",
          data: this.selCompanyDataAddressList.map((item) => item.name),
          axisLabel: {
            interval: 0,
            rotate: 45,
          },
          axisLabel: {
            color: "#333",
          },
        },
        yAxis: {
          type: "value",
          max: 100,
          axisLabel: {
            color: "#333",
          },
        },
        series: [
          {
            type: "bar",
            data: this.selCompanyDataAddressList.map((item) => item.count),
            itemStyle: {
              color: function (params) {
                const colorList = [
                  "#5470c6",
                  "#ee6666",
                  "#91cc75",
                  "#fac858",
                  "#73c0de",
                  "#9a60b4",
                  "#ff9896",
                  "#3ba272",
                  "#fc8452",
                  "#26c0c0",
                ];
                return colorList[params.dataIndex % colorList.length];
              },
            },
          },
        ],
      });
    },
  },
};
</script>

<style scoped></style>
